package operatingrecord

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/dao"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"strings"
)

//********************************************
//Func  ：查询操作记录
//Method：GET
//Usage ：
//********************************************
func QueryAllOpRecord(c *gin.Context) {
	pageSize, err1 := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	if err1 != nil {
		global.Log.Error("QueryAllOpRecord input param pagesize error:", zap.Any("error", err1))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	page, err2 := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err2 != nil {
		global.Log.Error("QueryAllOpRecord input param page error:", zap.Any("error", err2))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	startDate := c.DefaultQuery("startDate", "")
	endDate := c.DefaultQuery("endDate", "")
	employeeId, err := strconv.ParseInt(c.DefaultQuery("employeeId", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryAllOpRecord employee id is missing.")
	}

	total, records, err := dao.QueryOpRecordList(page, pageSize, employeeId, startDate, endDate)
	if err != nil {
		global.Log.Error("dao QueryAllOpRecord failed, Error:", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("未找到友情链接信息"))
		return
	}

	type OperatingRecordList struct { //列表查询输出
		Total int64
		List  []*entity.OperatingRecord
	}
	output := &OperatingRecordList{
		Total: total,
		List:  records,
	}

	global.Log.Info("QueryAllOpRecord success, output:", zap.Any("record list:", output))
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", output))
}

//********************************************
//Func  ：查询指定OpRecord详情
//Method：GET
//Param：recordId
//Usage ：
//********************************************
func QueryOpRecordDetail(c *gin.Context) {
	recordId, err := strconv.ParseInt(c.DefaultPostForm("recordId", ""), 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail("未指定操作员信息"))
		return
	}

	record, err := dao.QueryOpRecordDetail(recordId)
	if err != nil {
		global.Log.Error("QueryOpRecordDetail query declaration failed, Error:", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("未找到指定记录信息"))
		return
	}

	global.Log.Info("QueryOpRecordDetail operator info:", zap.Any("record:", record))
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", record))
}

//********************************************
//Func  ：添加新操作记录
//Method：POST
//Usage ：
//********************************************
func AddNewOpRecord(c *gin.Context) {
	var err error
	var record entity.OperatingRecord
	record.Event = strings.TrimSpace(c.DefaultPostForm("event", ""))
	record.EmployeeId, err = strconv.ParseInt(c.DefaultQuery("employeeId", ""), 10, 64)
	if err != nil {
		global.Log.Error("AddNewOpRecord employee id is missing.")
	}
	global.Log.Info("AddNewOpRecord: ", zap.Any("record:", record))

	err = dao.InsertOpRecord(record)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail("插入操作记录失败"))
		global.Log.Error("dao.InsertOpRecord failed:", zap.Any("error", err))
		return
	}

	global.Log.Info("AddNewOpRecord success.", zap.Any("new record", record))
	c.JSON(http.StatusOK, model.Success("操作成功"))
}
